package com.shxzz.easy12306.services.user.controller;

import com.shxzz.easy12306.framework.starter.common.dto.user.PassengerActualRespDTO;
import com.shxzz.easy12306.framework.starter.convention.result.Result;
import com.shxzz.easy12306.framework.starter.convention.result.Results;
import com.shxzz.easy12306.framework.starter.idempotent.annotation.Idempotent;
import com.shxzz.easy12306.framework.starter.idempotent.enums.IdempotentKeyType;
import com.shxzz.easy12306.framework.starter.idempotent.enums.IdempotentScene;
import com.shxzz.easy12306.framework.starter.user.core.UserContext;
import com.shxzz.easy12306.services.user.dto.req.PassengerRemoveReqDTO;
import com.shxzz.easy12306.services.user.dto.req.PassengerReqDTO;
import com.shxzz.easy12306.services.user.dto.resp.PassengerRespDTO;
import com.shxzz.easy12306.services.user.service.PassengerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 乘车人列表管理控制层
 * @author shxzz
 * @create 2024-03-02-20:41
 */
@RequiredArgsConstructor
@RestController
public class PassengerController {

    private final PassengerService passengerService;

    /**
     * 根据用户名查询乘车人脱敏信息列表
     */
    @GetMapping("/api/user-service/passenger/query")
    public Result<List<PassengerRespDTO>> listPassengerQueryByUsername() {
        return Results.success(passengerService.listPassengerQueryByUsername(UserContext.getUsername()));
    }

    /**
     * 根据乘车人 ID 集合,查询乘车人真实信息列表
     */
    @GetMapping("/api/user-service/inner/passenger/actual/query/ids")
    public Result<List<PassengerActualRespDTO>> listPassengerQueryByIds(@RequestParam("username") String username, @RequestParam("ids") List<Long> ids) {
        return Results.success(passengerService.listPassengerQueryByIds(username, ids));
    }

    @Idempotent(scene = IdempotentScene.RESTAPI, type = IdempotentKeyType.SPEL,
            uniqueKeyPrefix = "user-service:lock:save-passenger:",
            spel = "T(com.shxzz.easy12306.framework.starter.user.core.UserContext).getUsername()",
            message = "乘车人正在添加，请稍后再试...")
    @PostMapping("/api/user-service/passenger/save")
    public Result<Void> savePassenger(@RequestBody PassengerReqDTO requestParam) {
        passengerService.savePassenger(requestParam);
        return Results.success();
    }

    @Idempotent(scene = IdempotentScene.RESTAPI, type = IdempotentKeyType.SPEL,
            uniqueKeyPrefix = "user-service:lock:update-passenger:",
            spel = "T(com.shxzz.easy12306.framework.starter.user.core.UserContext).getUsername()",
            message = "乘车人正在修改，请稍后再试...")
    @PostMapping("/api/user-service/passenger/update")
    public Result<Void> updatePassenger(@RequestBody PassengerReqDTO requestParam) {
        passengerService.updatePassenger(requestParam);
        return Results.success();
    }

    @Idempotent(scene = IdempotentScene.RESTAPI, type = IdempotentKeyType.SPEL,
            uniqueKeyPrefix = "user-service:lock:remove-passenger:",
            spel = "T(com.shxzz.easy12306.framework.starter.user.core.UserContext).getUsername()",
            message = "乘车人正在删除，请稍后再试...")
    @PostMapping("/api/user-service/passenger/remove")
    public Result<Void> removePassenger(@RequestBody PassengerRemoveReqDTO requestParam) {
        passengerService.removePassenger(requestParam);
        return Results.success();
    }
}
